//
//  GCW_RankViewController.m
//  MooMusic
//
//  Created by 管晨雯 on 15/10/21.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "GCW_RankViewController.h"
#import "PrefixHeader.pch"
#import "GCW_RankViewCell.h"
#import "NetWork.h"
#import "GCW_RankModel.h"
// 导入头文件
#import "LTY_FilmAndSongsViewController.h"
@interface GCW_RankViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *rankTableView;
@property (nonatomic,strong) NSMutableArray *nameArr;/* 区标题数组 */
@property (nonatomic, strong)  NSMutableArray *refsArr;/* 接受数据的数组*/
@property (nonatomic, strong) NSMutableArray *titleArr; /*类型名称*/
@property (nonatomic, strong) NSMutableArray *imageArr;/* 背景图数组*/
@property (nonatomic, strong) NSMutableArray *songsArr;/*歌曲名数组*/
@end

static NSString *const cellIndertifier = @"cellIndertifier";

@implementation GCW_RankViewController

/* 初始化 */
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.nameArr = [NSMutableArray array];
        self.refsArr  = [NSMutableArray array];
        self.titleArr = [NSMutableArray array];
        self.imageArr = [NSMutableArray array];
        self.songsArr = [NSMutableArray array];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"排行榜";
    
    [self GetDate];
    [self creatTableView];
}

- (void)creatTableView
{
    self.rankTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)   style:UITableViewStylePlain];
    
    /* 签代理*/
    self.rankTableView.delegate = self;
    self.rankTableView.dataSource = self;
    
    /* 取消tableview的线 */
    self.rankTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
#pragma  mark -- 注册cell(重用标志)
    [self.rankTableView registerClass:[GCW_RankViewCell class] forCellReuseIdentifier:cellIndertifier];

    [self.view addSubview:self.rankTableView];
}


#pragma mark --- 获取网络数据
- (void)GetDate
{
    [NetWork networkGETRequestWithURL:RankPageUrl Paramater:nil ResultBlock:^(id result) {
       
    
        NSMutableArray *mArr = (NSMutableArray *)result;
        /* 分区的总标题*/
        for (NSMutableDictionary *dic in mArr) {
            [self.nameArr addObject:[dic objectForKey:@"name"]];
            
             /* 类型标题的数组 */
            NSMutableArray *tempArr = [NSMutableArray array];
            tempArr = [dic objectForKey:@"refs"];
            [self.refsArr addObject:tempArr];
           
//            NSLog(@"ppppppp %@", self.refsArr[0]);
            
       }
        
        if (self.nameArr.count > 0) {
            [self.rankTableView reloadData];
        }
        
    }];
}




#pragma mark --- 实现代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.refsArr[section] count];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.nameArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GCW_RankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndertifier];
    
    /* 在点击状态下禁止高亮 */
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    
    if (self.refsArr.count > 0) {
    
       if (indexPath.section == 0) {
           
           NSArray *arr = self.refsArr[indexPath.section];
           
           NSDictionary *refsDic = [arr objectAtIndex:indexPath.row];
           NSDictionary *dic = [refsDic objectForKey:@"image"];
           [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic"]] placeholderImage:nil];
           cell.typeLabel.text = [refsDic objectForKey:@"title"];
           
           
           NSArray *songsArr = [refsDic objectForKey:@"songs"];
           cell.nameLabel1.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:0] objectForKey:@"name"], [[songsArr objectAtIndex:0] objectForKey:@"singerName"]];
           cell.nameLabel2.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:1] objectForKey:@"name"], [[songsArr objectAtIndex:1] objectForKey:@"singerName"]];
           cell.nameLabel3.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:2] objectForKey:@"name"], [[songsArr objectAtIndex:2] objectForKey:@"singerName"]];
           
           
       } else if (indexPath.section == 1) {
           
           NSArray *arr = self.refsArr[indexPath.section];
           
           NSDictionary *refsDic = [arr objectAtIndex:indexPath.row];
           NSDictionary *dic = [refsDic objectForKey:@"image"];
           [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic"]] placeholderImage:nil];
           cell.typeLabel.text = [refsDic objectForKey:@"title"];
           
           NSArray *songsArr = [refsDic objectForKey:@"songs"];
           cell.nameLabel1.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:0] objectForKey:@"name"], [[songsArr objectAtIndex:0] objectForKey:@"singerName"]];
           
           if (self.songsArr.count > 0) {
               
               cell.nameLabel2.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:1] objectForKey:@"name"], [[songsArr objectAtIndex:1] objectForKey:@"singerName"]];
               
           }
           if (self.songsArr.count > 0) {
               
               cell.nameLabel3.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:2] objectForKey:@"name"], [[songsArr objectAtIndex:2] objectForKey:@"singerName"]];
               
           }
       } else if (indexPath.section == 2) {
           
           
           NSArray *arr = self.refsArr[indexPath.section];
           
           NSDictionary *refsDic = [arr objectAtIndex:indexPath.row];
           NSDictionary *dic = [refsDic objectForKey:@"image"];
           [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic"]] placeholderImage:nil];
           cell.typeLabel.text = [refsDic objectForKey:@"title"];
           
           NSArray *songsArr = [refsDic objectForKey:@"songs"];
           cell.nameLabel1.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:0] objectForKey:@"name"], [[songsArr objectAtIndex:0] objectForKey:@"singerName"]];
           
           if (self.songsArr.count > 0) {
            cell.nameLabel2.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:1] objectForKey:@"name"], [[songsArr objectAtIndex:1] objectForKey:@"singerName"]];
           }
           
           if (self.songsArr.count > 0) {
                cell.nameLabel3.text = [NSString stringWithFormat:@"%@ -%@", [[songsArr objectAtIndex:2] objectForKey:@"name"], [[songsArr objectAtIndex:2] objectForKey:@"singerName"]];
           }
           
       }
        
    }
    
    return cell;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *proHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] ;
       proHeader.backgroundColor = [UIColor colorWithRed:213 / 255.0
                                                green:209 / 255.0
                                                 blue:214 / 255.0
                                                alpha:1];
    proHeader.text = [self.nameArr objectAtIndex:section];
    return proHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * HEIGH;
}

/* Row的高度设置 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 200 * HEIGH;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTY_FilmAndSongsViewController *FilmVC = [[LTY_FilmAndSongsViewController alloc] init];
    
    NSArray *arr = self.refsArr[indexPath.section];
    
    NSDictionary *refsDic = [arr objectAtIndex:indexPath.row];
    
    /* songlist_id传到下一个界面*/
    NSString *string = [NSString stringWithFormat:@"%@",[refsDic objectForKey:@"songlist_id"]];
    NSString *stringUrl = [NSString stringWithFormat:HotSongString, string];
    FilmVC.DayString = stringUrl;
    
    FilmVC.nameString = [refsDic objectForKey:@"title"];
    FilmVC.descString = [refsDic objectForKey:@"desc"];
    
    // 把数据传递过去
    [self.navigationController pushViewController:FilmVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
